<?php
class CarAction extends CommonAction {
	public function _before_index() {
		$model = M ( "Car" );
		$list = $model->getField ( 'id,carno,cartype,engineno,buyinsuretime,audittime,factorymodel,driver,buydate,status,carimage,notes' );
		$this->assign ( 'list', $list );
	}
	public function _before_edit() {
		$model = M ( "Car" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
		$carrier=M('Carrier');
		$carrierlist=$carrier->select();
		$this->assign('carrier',$carrierlist);
	}
	public function read(){
		$model = M ( "Car" );
		$id=$_REQUEST['id'];
		$vo=$model->where('id='.$id)->find();
		$this->assign('vo',$vo);
		$this->display();
	}
	public function _before_add() {
		// $strDate = date ( "Y-m-d" );
		// $yearTime = $strDate - "1" . substr ( $strDate, 4 ); // 年审最早的时间
		// $this->assign ( "maxdate", $strDate );
		// $this->assign ( "mindate", $yearTime );
		$model = M ( "Car" );
		$maxid = $model->max ( 'id' ) + 1; // 获取唯一的序号
		$this->assign ( 'maxid', $maxid );
		$list = $model->select ();
		$this->assign ( 'list', $list );
		$carrier=M('Carrier');
		$carrierlist=$carrier->select();
		$this->assign('carrier',$carrierlist);
	}
	public function _before_insert() {
		$model = M ( "Car" );
		if ($model->where ( "id=" . $_POST ['id'] )->select ()) { // 判断车辆id是否存在
			$this->ajaxReturn ( '1', '车辆序号重复!', '300' );
			exit ();
		}
		$this->JudgeRepeat ();
		$this->getImage ();
	}
	
	public function _before_update() {
		$model = M ( "Car" );
		$list = $model->find ( $_POST ['id'] );
		$this->JudgeRepeat ( false );
		$this->getImage ();
	}
	
	/**
	 * +----------------------------------------------------------
	 * 在添加和修改时判断车牌号码是否重复
	 * +----------------------------------------------------------
	 *
	 * @param boolean $isAdd
	 *        	添加为true,修改为false
	 */
	public function JudgeRepeat($isAdd = true) {
		$model = M ( "Car" );
		$carno = $model->find ( $_POST ['id'] );
		if ($isAdd) {
			if ($model->where ( array (
					'carno' => $_POST ['carno'] 
			) )->select ()) {
				$this->ajaxReturn ( '1', '车牌号码重复!', '300' );
				exit ();
			}
		} else {
			if ($carno ['carno'] != $_POST ['carno']) {
				if ($model->where ( array (
						'carno' => $_POST ['carno'] 
				) )->select ()) {
					$this->ajaxReturn ( '1', '车牌号码重复!', '300' );
					exit ();
				}
			}
		}
	}
	/**
	 * +----------------------------------------------------------
	 * 获取用户在添加或修改时所选的车辆图片，若不选则有默认图片
	 */
	protected function getImage() {
		// if(empty($_FILES['carimage'])){
		// $this->ajaxReturn('1','请选择图片!','1');
		// exit();
		// }else{
		import ( "@.Org.UploadFile" );
		$upload = new UploadFile ();
		$upload->maxSize = 3145728;
		$upload->thumbMaxWidth = '300,500';
		$upload->thumbMaxHeight = '200,400';
		$upload->allowExts = array (
				'jpg',
				'gif',
				'png',
				'jpeg' 
		);
		$upload->savePath = "./Public/Uploads/CarImage/";
		$upload->saveRule = "time";
		if (! $upload->upload ()) {
		} else {
			// 上传成功，获取上传信息
			$info = $upload->getUploadFileInfo ();
		}
		// }
		if (empty ( $info )) {
			if (empty ( $_POST ['carimage'] )) {
				$imgurl = "/Uploads/CarImage/default.jpg";
			} else {
				$imgurl = $_POST ['carimage'];
			}
		} else {
			$savename = $info [0] ['savename'];
			$imgurl = "/Uploads/CarImage/" . $savename;
		}
		$_POST ['carimage'] = $imgurl;
		// $this->assign('carimg',$imgurl);
	}
	// 删除指定记录
	public function remove() {
		$id = $_REQUEST ['ids'];
		$Car = M ( 'Car' );
		$result = $Car->delete ( $id );
		if ($result) {
			$this->ajaxReturn ( '1', '删除成功！', 200 );
		} else {
			$this->error ( '1', '删除失败', '300' );
		}
	}
}